::p_load(sf,tidyverse,tmap,spdep,funModeling) pacman
In-class Exercise 2
Importing Geospatial Data
Importing water point geospatial data
<- st_read(dsn="geodata",
wp layer="geo_export",
crs=4326) %>%
filter(clean_coun == "Nigeria")
write_rds
is used to save the extracted sf data table into an output file in rds data format.
<- write_rds(wp,
wp_nga "geodata/wp_nga.rds")
Importing Nigeria LGA boundary data
<- st_read(dsn="geodata",layer="nga_admbnda_adm2_osgof_20190417",
nga crs=4326)
Data Wrangling
Recode NA values into string
The code chunk is used to recode all NA values into Unknown.
<- read_rds("geodata/wp_nga.rds") %>%
wp_nga mutate(status_cle = replace_na(status_cle, "Unknown"))
Explanatory Data Analysis
freq (data=wp_nga,input = "status_cle")
Extracting Water Point Data
Extracting functional water point
<- wp_nga %>%
wpt_functional filter(status_cle %in%
c("Functional",
"Functional but not in use",
"Functional but needs repair"))
freq(data=wpt_functional, input="status_cle")
Extracting non-functional water point
<- wp_nga %>%
wpt_nonfunctional filter(status_cle %in%
c("Abandoned/Decommissioned",
"Abandoned",
"Non-Functional",
"Non functional due to dry season",
"Non-Functional due to dry season"))
freq(data=wpt_nonfunctional,
input = 'status_cle')
Extracting water point with Unknown class
<- wp_nga %>%
wpt_unknown filter(status_cle == "Unknown")
Peforming Point-in-Polygon Count
<- nga %>%
nga_wp mutate(`total wpt` = lengths(
st_intersects(nga, wp_nga))) %>%
mutate(`wpt functional` = lengths(
st_intersects(nga, wpt_functional))) %>%
mutate(`wpt non-functional` = lengths(
st_intersects(nga, wpt_nonfunctional))) %>%
mutate(`wpt unknown` = lengths(
st_intersects(nga, wpt_unknown)))
Saving the analytical data table
<- nga_wp %>%
nga_wp mutate(pct_functional = `wpt functional`/`total wpt`) %>%
mutate(`pct_non-functional` = `wpt non-functional`/`total wpt`) %>%
select(3:4, 9:10, 18:23)
Now have the tidy sf data table and save it into rds format.
write_rds(nga_wp, "geodata/nga_wp.rds")
Visualising the spatial distribution of water points
<- read_rds("geodata/nga_wp.rds")
nga_wp <- qtm(nga_wp, "total wpt")
total <- qtm(nga_wp, "wpt functional")
wp_functional <- qtm(nga_wp, "wpt non-functional")
wp_nonfunctional <- qtm(nga_wp, "wpt unknown")
unknown
tmap_arrange(total, wp_functional, wp_nonfunctional, unknown, asp=0, ncol=2)